package cn.shop.merchant.controller.IpWhitelist;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.service.IpWhitelistService;
import cn.shop.transcation.ipWhitelistModule.entity.IpWhitelist;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * ip 白名单(IpWhitelist)表控制层
 *
 * @author jzw
 * @since 2022-02-23 11:04:03
 */
@Validated
@RestController
@RequestMapping("/ipWhitelist")
public class IpWhitelistController {

	/**
	 * 服务对象
	 */
	@Resource
	private IpWhitelistService ipWhitelistService;

	/**
	 * 查询所有数据
	 *
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@GetMapping("queryAll")
	public ApiResult<List<Object>> queryAll() {
		LambdaQueryWrapper<IpWhitelist> query = Wrappers.lambdaQuery();
		query.eq(IpWhitelist::getUserId, StpUtil.getSession().getLong(UID)).
				select(IpWhitelist::getIp);
		return ApiResult.success(ipWhitelistService.listObjs(query));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<IpWhitelist> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(ipWhitelistService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param ipList 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@PostMapping("insert")
	@OperationLog(desc = "修改ip白名单", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> insert(@RequestBody List<String> ipList) {
		return ApiResult.result(ipWhitelistService.insert(ipList));
	}


}

